import CanvasComponent, { CanvasComponentOptions } from "./CanvasComponent.js"

export default class RectFill extends CanvasComponent {
  fillStyle: string
  shadowColor?: string
  shadowBlur?: number
  shadowOffsetX?: number
  shadowOffsetY?: number
  borderRadius?: number | number[]
  borderColor?: string
  borderWidth?: number

  constructor(options: CanvasComponentOptions & {
    fillStyle?: string
    shadowColor?: string;
    shadowBlur?: number
    shadowOffsetX?: number
    shadowOffsetY?: number
    borderRadius?: number | number[]
    borderColor?: string
    borderWidth?: number
  }) {
    super(options)
    this.fillStyle = options.fillStyle || 'transparent'
    this.shadowColor = options.shadowColor
    this.shadowBlur = options.shadowBlur
    this.shadowOffsetX = options.shadowOffsetX
    this.shadowOffsetY = options.shadowOffsetY
    this.borderRadius = options.borderRadius
    this.borderColor = options.borderColor
    this.borderWidth = options.borderWidth
  }
  async draw(context: CanvasRenderingContext2D, dt: number): Promise<void> {
    if(context) {
      if(this.shadowColor) {
        context.shadowColor = this.shadowColor
        context.shadowBlur = this.shadowBlur ?? 0
        context.shadowOffsetX = this.shadowOffsetX ?? 0
        context.shadowOffsetY = this.shadowOffsetY ?? 0
      }
      context.fillStyle = this.fillStyle ?? 'transparent'
      context.strokeStyle = this.borderColor
      context.lineWidth = this.borderWidth
      if(this.borderRadius !== null && this.borderRadius !== undefined) {
        context.beginPath()
        context.roundRect(this.screenX, this.screenY, this.width, this.height, this.borderRadius)
        context.fill()
        if(this.borderColor && typeof this.borderWidth === 'number') {
          context.stroke()
        }
      } else {
        context.fillRect(this.screenX, this.screenY, this.width, this.height)
        if(this.borderColor && typeof this.borderWidth === 'number') {
          context.strokeRect(this.screenX, this.screenY, this.width, this.height)
        }
      }

      
    }
  }

  async init(): Promise<void> {
    await super.init()
    await this.resize()
  }

  async resize(): Promise<void> {
    await super.resize()
    this.inheritBoundary()
  }
}
